package com.syedu.cels.dao.impl;

import com.alibaba.fastjson.JSON;
import com.syedu.cels.dao.BaseDao;
import com.syedu.cels.entity.Exam;
import com.syedu.cels.entity.Result;
import com.syedu.cels.entity.Vocabulary;
import com.syedu.cels.entity.Word;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.syedu.cels.constants.Iconstants.*;

/**
 * @author easonsy
 * @Description
 * @createTime 2022/03/21 23:32:00
 */
public class BaseDaoImpl implements BaseDao {
    @Override
    public List<Word> selectAllWord() {
        List<Word> words = new ArrayList<>();
        Word word = null;
        try {
            List<String> lines = Files.readAllLines(Path.of(WORD_FILE_PATH));
            for (String line : lines) {
                word = new Word(line.split(" ")[0].toLowerCase(), line.split(" ")[1].split(":"));
                words.add(word);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return words;
    }

    @Override
    public List<Vocabulary> selectAllVocabularies() {
        List<Vocabulary> vocabularies = new ArrayList<>();
        Vocabulary vocabulary = null;
        try {
            List<String> lines = Files.readAllLines(Path.of(VOCABULARY_FILE_PATH));
            for (String line : lines) {
                String[] arr = line.split("#");
                if (arr.length <= 2) {
                    vocabulary = new Vocabulary(arr[0], arr[1].split(":"));
                } else {
                    vocabulary.setAbbreviation(arr[2]);
                }
                vocabularies.add(vocabulary);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return vocabularies;
    }

    @Override
    public List<Exam> generateTestPaper() {
        List<Exam> exams = new ArrayList<>();
        Exam examWord = null;
        Exam examVocabulary = null;
        try {
            List<String> lines1 = Files.readAllLines(Path.of(WORD_FILE_PATH));
            for (String line1 : lines1) {
                String[] arr1 = line1.split(" ");
                examWord = new Exam(arr1[0], arr1[1]);
                exams.add(examWord);
            }
            List<String> lines2 = Files.readAllLines(Path.of(VOCABULARY_FILE_PATH));
            for (String line2 : lines2) {
                String[] arr2 = line2.split("#");
                examVocabulary = new Exam(arr2[0], arr2[1]);
                exams.add(examVocabulary);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Collections.shuffle(exams);
        return exams;
    }

    @Override
    public void collectResult(Result result) {
        try (PrintWriter printWriter = new PrintWriter(new FileWriter(TEST_RESULT, true), true)) {
            printWriter.println(JSON.toJSONString(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
